﻿@using Smartstore.Utilities

@functions{
    private bool? Value
    {
        get
        {
            bool? value = null;
            if (ViewData.Model != null)
            {
                value = Convert.ToBoolean(ViewData.Model, System.Globalization.CultureInfo.InvariantCulture);
            }
            return value;
        }
    }

    private List<SelectListItem> TriStateValues
    {
        get
        {
            return new List<SelectListItem>
            {
                new SelectListItem { Text = T("Common.Unspecified"), Value = string.Empty, Selected = !Value.HasValue },
                new SelectListItem { Text = T("Common.Yes"), Value = "true", Selected = Value.HasValue && Value.Value },
                new SelectListItem { Text = T("Common.No"), Value = "false", Selected = Value.HasValue && !Value.Value },
            };
        }
    }
}

@{
    var attributes = new AttributeDictionary().Merge(ConvertUtility.ObjectToDictionary(ViewData["htmlAttributes"] ?? new object()));

    // Hack to force the missing "checked" when calling the GET action method directly in case of an invalid ModelState.
    @if(!ViewData.ModelMetadata.IsNullableValueType && !attributes.ContainsKey("checked") && (Value ?? false) == true)
    {
        attributes["checked"] = "checked";
    }
}

@if (!ViewData.ModelMetadata.IsNullableValueType)
{
    <input asp-for="@Model" 
           attr-value="(Value ?? false, (Value ?? false).ToString())" 
           class="check-box form-check-input" 
           type="checkbox" 
           attrs="attributes" />
}
else
{
    var size = GetMetadata<string>("size");

    <select id="@Html.IdForModel()" 
            name="@Html.NameForModel()" 
            asp-items="TriStateValues" 
            class="list-box tri-state form-control edit-control"
            attr-class='(size.HasValue(), "form-control-" + size)'
            attrs="attributes" 
            data-editor="select"></select>
}